package mysql

import (
	"home-furnish/homerpc/homerpc"

	"gorm.io/gorm"
)

type Home struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(30);not null;comment:'家具名称'"`
	Image   string  `gorm:"type:varchar(500);not null;comment:'家具图片'"`
	Price   float64 `gorm:"type:decimal(10,2);not null;comment:'家具价格'"`
	ClassId int64   `gorm:"type:int(11);not null;comment:'分类id'"`
	BrandId int64   `gorm:"type:int(11);not null;comment:'品牌id'"`
}

func (h *Home) HomeAdd(db *gorm.DB) error {
	return db.Debug().Create(h).Error
}

func (h *Home) HomeShow(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Find(h).Error
}

func HomeList(db *gorm.DB, lists []*homerpc.HomeList) []*homerpc.HomeList {
	/*
		SELECT
		homes.id as home_id,homes.name,image,price,classes.name as class_name,brands.name as brand_name
		FROM
		homes
		 JOIN brands ON homes.brand_id=brands.id
	*/
	db.Debug().Model(&Home{}).
		Select("homes.id as home_id,homes.name,image,price,classes.name as class_name,brands.name as brand_name").
		Joins("JOIN brands ON homes.brand_id=brands.id").
		Joins("JOIN classes ON homes.class_id=classes.id").
		Find(&lists)
	return lists
}
